package chaowan.Mapper;

import chaowan.pojo.ShopUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ShopUserMapper {

    @Select("SELECT * FROM shop_user")
    List<ShopUser> selectAll();

    @Select("SELECT * FROM shop_user WHERE uid = #{uid}")
    ShopUser selectById(Long uid);

    @Insert("INSERT INTO shop_user(uname, pwd, email, type, active, created_at) " +
            "VALUES(#{uname}, #{pwd}, #{email}, #{type}, #{active}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "uid")
    int insert(ShopUser user);

    @Update("UPDATE shop_user SET uname=#{uname}, pwd=#{pwd}, email=#{email}, type=#{type}, active=#{active} WHERE uid=#{uid}")
    int update(ShopUser user);

    @Delete("DELETE FROM shop_user WHERE uid = #{uid}")
    int deleteById(Long uid);

    // 根据用户名和密码查找用户（登录用）
    @Select("SELECT * FROM shop_user WHERE uname = #{uname} AND pwd = #{pwd} LIMIT 1")
    ShopUser findByUsernameAndPassword(@Param("uname") String uname, @Param("pwd") String pwd);

    // 根据用户名查找用户（注册时检查用户名是否已存在）
    @Select("SELECT * FROM shop_user WHERE uname = #{uname} LIMIT 1")
    ShopUser findByUsername(@Param("uname") String uname);

    // 插入新用户（注册用）
    @Insert("INSERT INTO shop_user(uname, pwd, email, type, active, created_at) VALUES(#{uname}, #{pwd}, #{email}, #{type}, #{active}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "uid")
    int insertUser(ShopUser user);
}